"""myDjango URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/3.1/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.shortcuts import render, redirect
from django.urls import path
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from django.views import View
from myDjango.http.response import JsonResponseCn
from django.conf.urls import include, url
## 视图函数

class HelloView(View):

    def get(self, request, *args, **kwargs):
        request_info = ""
        request_info += "request.scheme={}".format(request.scheme)
        request_info += "request.body={}".format(request.body)
        request_info += "request.path={}".format(request.path)
        request_info += "request.method={}".format(request.method)
        request_info += "request.GET={}".format(request.GET)
        request_info += "request.FILES={}".format(request.FILES)

        request_info += "request.get_host={}".format(request.get_host())
        request_info += "request.get_port={}".format(request.get_port())
        request_info += "request.get_full_path={}".format(request.get_full_path())
        request_info += "request.get_raw_uri={}".format(request.get_raw_uri())
        request_info += "request.build_absolute_uri={}".format(request.build_absolute_uri())
        print(request_info)
        return HttpResponse('get\n')


    def post(self, request, *args, **kwargs):
        request_info = ""
        request_info += "request.scheme={}".format(request.scheme)
        request_info += "request.body={}".format(request.body)
        request_info += "request.path={}".format(request.path)
        request_info += "request.method={}".format(request.method)
        request_info += "request.POST={}".format(request.POST)
        request_info += "request.FILES={}".format(request.FILES)

        request_info += "request.get_host={}".format(request.get_host())
        request_info += "request.get_port={}".format(request.get_port())
        request_info += "request.get_full_path={}".format(request.get_full_path())
        request_info += "request.get_raw_uri={}".format(request.get_raw_uri())
        request_info += "request.build_absolute_uri={}".format(request.build_absolute_uri())
        print(request_info)
        return HttpResponse('post\n')

    def put(self, request, *args, **kwargs):
        request_info = ""
        request_info += "request.scheme={}".format(request.scheme)
        request_info += "request.body={}".format(request.body)
        request_info += "request.path={}".format(request.path)
        request_info += "request.method={}".format(request.method)
        request_info += "request.PUT={}".format(request.PUT)
        request_info += "request.FILES={}".format(request.FILES)

        request_info += "request.get_host={}".format(request.get_host())
        request_info += "request.get_port={}".format(request.get_port())
        request_info += "request.get_full_path={}".format(request.get_full_path())
        request_info += "request.get_raw_uri={}".format(request.get_raw_uri())
        request_info += "request.build_absolute_uri={}".format(request.build_absolute_uri())
        print(request_info)
        return HttpResponse('put\n')

    def delete(self, request, *args, **kwargs):
        request_info = ""
        request_info += "request.scheme={}".format(request.scheme)
        request_info += "request.body={}".format(request.body)
        request_info += "request.path={}".format(request.path)
        request_info += "request.method={}".format(request.method)
        request_info += "request.DELETE={}".format(request.DELETE)
        request_info += "request.FILES={}".format(request.FILES)

        request_info += "request.get_host={}".format(request.get_host())
        request_info += "request.get_port={}".format(request.get_port())
        request_info += "request.get_full_path={}".format(request.get_full_path())
        request_info += "request.get_raw_uri={}".format(request.get_raw_uri())
        request_info += "request.build_absolute_uri={}".format(request.build_absolute_uri())
        print(request_info)
        return HttpResponse('delete\n')

    # 注意，给CBV加上@csrf_exempt注解，需要加到dispatch方法上，后续会详解介绍这个函数的作用
    @csrf_exempt
    def dispatch(self, request, *args, **kwargs):
        return super(HelloView, self).dispatch(request, *args, **kwargs)

def redirect_view1(request):
    # 忽略其他
    return redirect("http://www.baidu.com")

def index(request, *args, **kwargs):
    return render(request, "index.html",{"title":"听风Blog"})

urlpatterns = [
    path('admin/', admin.site.urls),
    ## 添加的url映射，由上面的hello_world()函数处理
    path('hello/',HelloView.as_view()),
    path('json/',JsonResponseCn.hello_json),
    path('index/',index),
    path('redirect_view1/',redirect_view1),
    path('hello_view/',include('myDjango.hello_app.urls'))
]

